package com.mmall.util;

import com.mmall.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

/**
 * 封装分片redis
 */
@Slf4j
public class RedisShardedPoolUtil {


    /**
     * 设置对应key的有效期
     * @param key
     * @param exTime 有效期，单位秒
     * @return
     */
    public static Long expire(String key, int exTime){
        ShardedJedis shardedJedis = null;
        Long result = null;
        try{
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.expire(key,exTime);
        }catch (Exception e){
            log.error("set key:{} exTime:{} value:{} error",key,exTime,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    /**
     * string 添加，存在有效期exTime
     * @param key 键
     * @param value 值
     * @param exTime 有效期，单位秒
     * @return
     */
    public static String setEx(String key, String value, int exTime){
        ShardedJedis shardedJedis = null;
        String result = null;
        try{
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.setex(key,exTime,value);
        }catch (Exception e){
            log.error("set key:{} exTime:{} value:{} error",key,exTime,value,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    /**
     * string 添加
     * @param key
     * @param value
     * @return
     */
    public static String set(String key, String value){
        ShardedJedis shardedJedis = null;
        String result = null;
        try{
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.set(key,value);
        }catch (Exception e){
            log.error("set key:{} value:{} error",key,value,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    /**
     * string 获取
     * @param key
     * @return
     */
    public static String get(String key){
        ShardedJedis shardedJedis = null;
        String result = null;
        try{
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.get(key);
        }catch (Exception e){
            log.error("get key:{} error",key,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    /**
     * stirng 删除
     * @param key
     * @return
     */
    public static Long del(String key){
        ShardedJedis shardedJedis = null;
        Long result = null;
        try{
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.del(key);
        }catch (Exception e){
            log.error("get key:{} error",key,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

//    public static void main(String[] args) {
//        Jedis jedis = RedisShardedPool.getJedis();
//        RedisShardedPoolUtil.set("age","23");
//        String result = RedisShardedPoolUtil.get("name");
//        System.out.println("name的结果是：" + result);
//        RedisShardedPoolUtil.setEx("address","北京",1000*60);
//        RedisShardedPoolUtil.expire("name", 1000*60);
//        System.out.println(RedisShardedPoolUtil.del("address"));
//    }
}